home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d17 / print.arc / PRINT.C < prev    next >
Text File  |  1987-06-13  |  18KB  |  530 lines

  1. /*
  2.  
  3.               Printfile Utility - by Mark J. Quarles
  4.  
  5.             Copyright (c) 1985, 1986 by Mark J. Quarles
  6.  
  7.          All Commercial and non-commercial rights reserved.
  8.  
  9.  
  10.    ************************* NOTICE *********************************
  11.    *                                                                *
  12.    *  This program is protected under Title 17 of the United States *
  13.    *  Legal Code.  All commercial and non-commercial rights and     *
  14.    *  uses of this program product are regulated by and the property*
  15.    *  of the author.                                                *
  16.    *                                                                *
  17.    *  DISCLOSURE of the source code for this program to third       *
  18.    *  parties is EXPRESSLY PROHIBITED unless permission in writing  *
  19.    *  is obtained from the author.                                  *
  20.    *                                                                *
  21.    ******************************************************************
  22.  
  23.  
  24.    ************************ Modification Notes ***************************
  25.  
  26.    June 1986 - Added default handling on all inputs in the modification
  27.                mode.  Added environment handling and printer configuration
  28.                file.  Developed companion program PINSTALL.
  29.  
  30.    **********************************************************************
  31.  
  32.  
  33.   This program will output a sourcefile to the printer, with headings
  34. and page numbers.  Page breaks are supplied by the program at the
  35. end of the page.
  36.  
  37. Invoke with:     PRINT <filespec> <m>
  38.  
  39.           where <filespec> is the file (text or doc) to print, and
  40.                 <m> indicates whether or not you desire to modify
  41.                 the printer control data.
  42.  
  43. */
  44.  
  45. #include <stdlib.h>
  46. #include <dos.h>
  47. #include <process.h>
  48. #include <conio.h>
  49. #include <stdio.h>
  50. #include <signal.h>
  51. #include <string.h>
  52.  
  53. /*************************************************************************
  54.    Procedure to allow the user to set the left margin.  The current value
  55.    of the left margin is passed to this procedure.  If they select to
  56.    change the margin, the new value is returned.  Otherwise, the original
  57.    value is returned.
  58. **************************************************************************/
  59.  
  60. int margin_set(left_margin)
  61. int left_margin;
  62. {
  63. int c;
  64.  
  65. /************************************************************************
  66.       left_margin  - value of the left margin
  67.       c            - scratchpad
  68. *************************************************************************/
  69.  
  70. cls();
  71.  
  72. printf("Current left margin is set for %d\n",left_margin);
  73. printf("Do you wish to set the left margin (y/N): ");
  74. c=getche();
  75. if ((c=='y') || (c=='Y')) {
  76.   printf("\nEnter new left margin: ");
  77.   scanf("%d",&left_margin);
  78.   }
  79.  
  80.  
  81. return (left_margin);
  82. }
  83.  
  84. /************************************************************************
  85. *************************    Main Program Code   ************************
  86. ************************************************************************/
  87.  
  88. main(argc,argv)
  89. int argc;
  90. char *argv[];
  91. {
  92. int code0[10],code1[10],code2[10],code3[10]; /* printer configuration control codes */
  93. int notice,linecount,pagelength,bottommargin,linelimit,c;
  94. int code_on,file_count,code_count,max_len,code0t,code1t,code2t,code3t;
  95. int j,row,header,left_margin,i,marg,linenum,page,escape,pause;
  96. char file[80],*cpyrte_name[80],*cpyrte_date[80];
  97. char subdir[20],*sub_dir,temp[30],data[26],label[80];
  98. FILE *f1,*f2;
  99.  
  100. /**************************************************************************
  101.  
  102.    code0[]      - First printer control code
  103.    code1[]      - second printer control code
  104.    code2[]      - third printer control code
  105.    code3[]      - fourth printer control code
  106.    notice       - Flag for copyright notice [1=>yes, 0=>no]
  107.    linecount    - count of current lines printed on the page
  108.    pagelength   - length (in lines) of a page (form length)
  109.    bottommargin - bottom margin (in lines)
  110.    linelimit    - maximum characters to print per line
  111.    c            - scratchpad
  112.    code_on      - flag to indicate that they are using control codes
  113.                   other than the default
  114.    file_count   - scratchpad used in counting chars for longest line in file
  115.    code_count   - total number of control codes in the configuration file
  116.    max_len      - maximum characters per line in the sourcefile
  117.    code0t       - temporary input storage during configuration file read
  118.    code1t       -     ditto
  119.    code2t       -       ditto
  120.    code3t       -         ditto
  121.    j            - scratchpad
  122.    row          - current cursor position
  123.    header       - flag on whether or not to print the filename as a header
  124.    left_margin  - value of the left margin
  125.    i            - scratchpad
  126.    marg         - counter for spacing the printout to the margin setting
  127.    linenum      - count (in lines) of linecount + header lines
  128.    page         - current page counter
  129.    escape       - flag for escape codes [ 1= on, 0 => off]
  130.    pause        - flag for end-of-page pause [1 => on, 0=> off]
  131.    file         - filename to print
  132.    cpyrte_name  - name of program for copyrite procedure
  133.    cpyrte_date  - date of copyright for copyrite procedure
  134.    subdir       - string containing the path to the configuration file
  135.    sub_dir      - address of the environment variable PRINT
  136.    temp         - character array for use in reading the configuration file
  137.    data         - character array for third command line parameter
  138.    label        - printer name from configuration file
  139.    f1           - stream pointer to sourcefile
  140.    f2           - stream pointer to printer
  141.  
  142. ***************************************************************************
  143.  
  144.    Look for the printer configuration file.  If it doesn't exist, then
  145.    tell them and abort.  Get the subdirectory for the configuration
  146.    file from the environment table.
  147.  
  148. **************************************************************************/
  149.  
  150.  
  151. strncpy(file,"printer.cnf\0",13); /* set up the config file name */
  152. sub_dir = getenv("PRINT");  /* find the environmnet variable for print */
  153. strncpy(subdir,sub_dir,20);
  154. i=strlen(subdir);
  155. if ((subdir[i-1] !=92) && (i>0)) {
  156.   subdir[i] = 92;   /* put in a backslash */
  157.   ++i;
  158.   }
  159.  
  160. for (j=0; j<12; j++)
  161.   subdir[i++] = file[j]; /* copy the rest of the name to it */
  162.  
  163.  
  164. if ((f1=fopen(subdir,"r")) == NULL) { /* open the configuration file */
  165.  printf("\n\n");
  166.  cprint("Error: Printer configuration file not found ",131);
  167.  printf("\nConfiguration file is %s",subdir);
  168.  printf("\n\nYou must run the PINSTALL procedure first.\n\n");
  169.  exit(1);
  170.  }
  171.  
  172. /*********************************************************************
  173.    Display a menu of configuration codes from the configuration file.
  174. **********************************************************************/
  175.  
  176. code_on = -1; /* code selection disabled */
  177. i=0;
  178. fgets(label,80,f1); /* get the printer name */
  179. printf("Loading configuration for %s\n\n",label);
  180.  
  181. while ((c=getc(f1)) != EOF) {
  182.  ungetc(c,f1);
  183.  fgets(temp,20,f1); /* get the code label */
  184. if (argc == 3)
  185.  printf("%2d. %s",i,temp);
  186.  
  187.  fscanf(f1,"%d %d %d %d",&code0t,&code1t,&code2t,&code3t);
  188.  code0[i]=code0t;
  189.  if (code0[i] == 1)
  190.   code0[i] = 27;
  191.  code1[i]=code1t;
  192.  code2[i]=code2t;
  193.  code3[i]=code3t;
  194.  ++i;
  195.  }
  196. code_count =i-1;
  197. fclose(f1);
  198.  
  199. header = 1; /* default to printing sourcefile name as a header */
  200.  
  201. /**************************************************************************
  202.   If they supplied the third parameter on the command line, let them
  203.   select the configuration code they want to print with.
  204. ***************************************************************************/
  205.  
  206. if (argc == 3) {
  207.  printf("[Note: Option 0 corresponds to NO CHANGE in default printer code]\n");
  208.  printf("\nWhich printer option should be used (0, or 3 to %d )? ",code_count);
  209. row=get_position(0);
  210. code_on = -1;
  211. while (((code_on <3) || (code_on >code_count)) && (code_on != 0)) {
  212.     scanf("%d",&code_on);
  213.     if ((code_on <2) || (code_on > code_count)) {
  214.          set_position(row,0);
  215.          printf("Which printer option should be used (0, or 3 to %d)? ",code_count);
  216.          }
  217.     }
  218. if (code_on != 0)
  219.  header = 0; /* turn the header off */
  220.  printf("\n");
  221.  }
  222.  
  223. left_margin = 3; /* set the left margin to 3 */
  224. marg = 0;
  225.  if (code0 == 0)
  226.    escape = 0; /* turn off escape code mode */
  227.   else
  228.    escape = 1;
  229. notice = 1; /* turn on the copyright footnote */
  230. pause = 0; /* default to no pause */
  231.  
  232.  
  233. /**************************************************************************
  234.   If they didn't supply the sourcefile name on the command line, give them
  235.   an informational message and abort.
  236. ***************************************************************************/
  237.  
  238. if (argc == 1) {
  239.   copyrite("PRINT\0","December 1985\0");
  240.   cls();
  241.   cprint("Error: Sourcefile not specified",140);
  242.   printf("\n\nThis program will print a file in formatted, paged,\n");
  243.   printf("format.  You must invoke this procedure as:\n\n");
  244.   printf("PRINT <sourcefile>  Where <sourcefile> is the name\n");
  245.   printf("                    of the file you wish to print\n\n");
  246.   printf("\nOptionally, you may invoke this program as follows:\n\n");
  247.   printf("PRINT <sourcefile> M   Where the 'M' indicates that you\n");
  248.   printf("                       wish to modify the printer control\n");
  249.   printf("                       characters used to select the enhanced\n");
  250.   printf("                       print mode, margins, lines per page\n");
  251.   printf("                       headers, footers, or other optional\n");
  252.   printf("                       parameters.\n\n");
  253.   exit(0);
  254.   }
  255.  
  256. if ((f1=fopen(argv[1],"r")) == NULL) { /* open the sourcefile */
  257.   cls();
  258.   cprint("Error: Cannot open file",140);
  259.   printf(" %s\n\n",argv[1]);
  260.   printf("This program will print a file in formatted, paged,\n");
  261.   printf("format.  You must invoke this procedure as:\n\n");
  262.   printf("PRINT <sourcefile>  Where <sourcefile> is the name\n");
  263.   printf("                    of the file you wish to print\n\n");
  264.   exit(0);
  265.   }
  266.  
  267. file_count = max_len=0; /* get the longest line length */
  268. cls();
  269.  
  270. /************************************************************************
  271.    Find the length of the longest line in the sourcefile
  272. *************************************************************************/
  273.  
  274. printf("\nEvaluating sourcefile - Please standby ");
  275. while ((c=getc(f1)) != EOF) {
  276.  if (c != '\n')
  277.    ++file_count;
  278.   if (c == '\n') {
  279.     printf(".");
  280.     if (file_count > max_len)
  281.       max_len=file_count;
  282.     file_count =0;
  283.     }
  284.   }
  285.  
  286. fclose(f1);
  287. if ((f1=fopen(argv[1],"r")) == NULL) {
  288.   printf("\nError: Unable to reopen sourcefile\n\n");
  289.   exit(1);
  290.   }
  291.  
  292. if ((f2=fopen("PRN","w")) == NULL) { /* open a stream to the printer */
  293.   cls();
  294.   cprint("Error: Printer is not available",140);
  295.   printf("\n\n");
  296.   fclose(f1);
  297.   exit(0);
  298.   }
  299.  
  300. strncpy(file,argv[1],80);
  301. for (i=0; i<=strlen(file); i++)
  302.  file[i]=toupper(file[i]);
  303.  
  304. linecount = linenum = 0;
  305. page = 1;
  306. pagelength = 66; /* number of lines per page */
  307. bottommargin = 54; /* number of lines to print per page */
  308.  
  309. linelimit = 74;
  310.  
  311. if (argc == 3)
  312.   strncpy(data,argv[2],1); /* fetch the modifier on the command line */
  313.  
  314. /*************************************************************************
  315.   If they supplied the optional 'm' parameter on the command line, proceed
  316.   to prompt them for all of the modifiable parameters.
  317. **************************************************************************/
  318.  
  319. if ((argc == 3) && ((data[0] == 'm') || (data[0] == 'M'))) {
  320.  
  321. left_margin = margin_set(left_margin);
  322. linelimit -=left_margin;
  323.  
  324. printf("\n\nCurrent lines per page is set for %d\n",bottommargin);
  325. printf("Do you wish to set the number of lines to print per page (y/N)? ");
  326. c=getche();
  327. printf("\n");
  328.  
  329. if ((c == 'Y') || (c=='y')) {
  330.   printf("\nEnter number of lines to print per page: ");
  331.   scanf("%d",&bottommargin);
  332.   }
  333.  
  334. printf("\n\nCurrent characters per line is %d (excluding margin)\n",linelimit);
  335. printf("Longest line in the sourcefile is %d characters\n",max_len);
  336. printf("Do you wish to set the characters per line (y/N)? ");
  337. c=getche();
  338. printf("\n");
  339. if ((c == 'Y') || (c=='y')) {
  340.   printf("\nEnter number of characters to print per line: ");
  341.   scanf("%d",&linelimit);
  342.   }
  343.  
  344. printf("\n\nCurrent control characters are: \n");
  345. printf("                Enlarge print mode: %2d %2d %2d %2d\n",code0[1],code1[1],code2[1],code3[1]);
  346. printf("                Normal print mode:  %2d %2d %2d %2d\n",code0[2],code1[2],code2[2],code3[2]);
  347. printf("\nDo you wish to change the control characters (y/N)? ");
  348. c=getche();
  349. printf("\n");
  350. if ((c =='Y') || (c=='y')) {
  351.    printf("\nComplete reconfiguration is available via the PINSTALL command\n\n");
  352.    printf("\nEnter a SINGLE character code for ENLARGED print mode: ");
  353.    scanf("%d",&code1[1]);
  354.    code2[1]=code3[1]=0;
  355.    printf("\nEnter a SINGLE character code for NORMAL print mode:   ");
  356.    scanf("%d",&code1[2]);
  357.    code2[2]=code3[2]=0;
  358.    }
  359.  
  360. if ((c=='y') || (c=='Y')) {
  361. if (escape == 0)
  362.  printf("\n\nCurrently set to NOT send escape code prefix.");
  363.  else
  364.  printf("\n\nCurrently set to send escape code prefix.");
  365. printf("\nDo you require an ESCAPE (ASCII 27)before these codes to\n");
  366. printf("switch print modes (y/N)? ");
  367. c=getche();
  368. printf("\n");
  369. if ((c == 'Y') || (c=='y')) {
  370.     code0[0]=code0[1]=code0[2]=code0[3]=code0[4]=code0[5]=27;
  371.     code0[6]=code0[7]=code0[8]=code0[9]=code0[10]=27;
  372.     escape = 1;
  373.     }
  374.    }
  375.  printf("\n\nDo you want to pause after each page (y/N)? ");
  376.  c=getche();
  377.  printf("\n");
  378.  if ((c =='Y') || (c=='y')) {
  379.     pause = 1; /* turn on the pause mode */
  380.     }
  381.  
  382.  printf("\n\nDo you want the copyright footnote (Y/n)? ");
  383.  c=getche();
  384.  printf("\n");
  385.  if ((c=='n') || (c=='N')) {
  386.    notice = 0; /* turn off the copyright notice */
  387.    bottommargin +=3; /* increase bottom margin by 3 */
  388.    }
  389.  
  390.  
  391. if (header == 1) {
  392. printf("\n\nDo you want the sourcefile name as a header (Y/n): ");
  393. c=getche();
  394. if ((c=='n') || (c=='N'))
  395.   header = 0; /* turn off the header */
  396.   }
  397.  
  398.   } /* end of if argc==3 */
  399.  
  400.  
  401. /**************************************************************************
  402.    Now print out the sourcefile
  403. ***************************************************************************/
  404.  
  405. cls();
  406. printf("\nPrintfile Utility - by Mark J. Quarles \n\n");
  407. printf("\nUsing configuration for %s\n",label);
  408. printf("\nPrinting file: %s\n\n",file);
  409.  
  410. putc(code0[0],f2);
  411. putc(code1[0],f2);
  412. putc(code2[0],f2);
  413. putc(code3[0],f2); /* send out the printer initialization string */
  414.  
  415. putc('\n',f2);
  416.  
  417. if (code_on > -1) {
  418.   putc(code0[code_on],f2);
  419.   putc(code1[code_on],f2);
  420.   putc(code2[code_on],f2);
  421.   putc(code3[code_on],f2);
  422.   }
  423.  
  424. if (header ==1) { /* print the sourcefile name as an enlarged string */
  425.  
  426. putc(code0[1],f2);
  427. putc(code1[1],f2);
  428. putc(code2[1],f2);
  429. putc(code3[1],f2); /* go to enlarged mode */
  430. fprintf(f2,"                 %s\n",file);
  431. putc(code0[2],f2);
  432. putc(code1[2],f2);
  433. putc(code2[2],f2);
  434. putc(code3[2],f2); /* return to normal mode */
  435. putc('\n',f2);
  436. }
  437. else {
  438.  fprintf(f2,"\n\n\n\n\n");
  439.  }
  440.  
  441. linenum = 5;
  442.  
  443. for (marg=0; marg<left_margin; marg++)
  444.  putc(32,f2);
  445.  
  446. while (((c=getc(f1)) == '\n') || (c==10));
  447.          /* strip off leading carriage returns in line above*/
  448. ungetc(c,f1); /* put back the last character we got */
  449.  
  450. /*******************************
  451.   Here is our main printing loop
  452. ********************************/
  453.  
  454. while ((c=getc(f1)) != EOF) {
  455.   linecount +=1; /* increment the linecount */
  456.   if ((linecount >=linelimit)&& (c==' ')) {
  457.      putc('\n',f2); /* send a carriage return */
  458.      for (marg=0; marg<left_margin; marg++)
  459.       putc(32,f2);
  460.      linecount = 0; /* reset the line count */
  461.      linenum +=1; /* increment the line  number count */
  462.       }
  463.   if ((linenum >=bottommargin)|| (c==12)) {
  464.       if (c==12) {
  465.          c==0;
  466.          for (marg=linenum; marg<=bottommargin; ++marg)
  467.            putc('\n',f2); /* space down to the proper place */
  468.          }
  469.       putc('\n',f2);
  470.       fprintf(f2,"                             Page %d\n",page);
  471.     if (notice == 1)
  472.       fprintf(f2,"\nCopyrighted material - Disclosure to third parties is prohibited.\n");
  473.       page +=1;
  474.       putc(12,f2); /* send a form feed */
  475.       if (pause == 1) {
  476.         printf("Press any key to begin printing page %d: ",page);
  477.         c=getch();
  478.         printf("\n");
  479.         }
  480.       putc('\n',f2);
  481.  
  482.     if (header == 1) {
  483.  
  484.       putc(code0[1],f2);
  485.       putc(code1[1],f2);
  486.       putc(code2[1],f2);
  487.       putc(code3[1],f2); /* enlarged mode */
  488.       fprintf(f2,"                 %s\n",file);
  489.       putc(code0[2],f2);
  490.       putc(code1[2],f2);
  491.       putc(code2[2],f2);
  492.       putc(code3[2],f2); /* normal mode */
  493.       putc('\n',f2);
  494.       }
  495.       else
  496.         fprintf(f2,"\n\n\n\n\n");
  497.       for (marg=0; marg<left_margin; marg++)
  498.         putc(32,f2);
  499.       linenum = 5; /* reset the line number */
  500.       }
  501.  
  502.   if (c == '\n') {
  503.     putc(c,f2);
  504.     linenum +=1;
  505.     linecount = 0;
  506.     for (marg=0; marg<left_margin; marg++)
  507.       putc(32,f2);
  508.     }
  509.   if (c==9) {
  510.     for (c=0; c<4; c++)
  511.         putc(32,f2); /* tab expansion */
  512.     c=9;
  513.     }
  514.  
  515.   if ((c!='\n')&& (c!=9)) /* exclude carriage returns and tabs */
  516.    putc(c,f2); /* print the character */
  517.   }
  518. putc('\n',f2);
  519.  
  520. for (marg=linenum; marg<=bottommargin; marg++)
  521.  putc('\n',f2);
  522.  
  523. fprintf(f2,"                             Page %d\n",page);
  524. putc(12,f2);
  525. }
  526.  
  527. arg<=bottommargin; marg++)
  528.  putc('\n',f2);
  529.  
  530. fprintf(f2,"                             Page %d\n",p